package com.mall.huohuo.aspect.annotation;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author zyconding
 * @version AuthParam.java, v 0.1 2020-08-07 16:23 zyconding
 */
@Aspect
@Component
@Slf4j
public class LogAspect {


    @Pointcut(value =
            "@within(org.springframework.stereotype.Service) || @annotation(Log)")
    public void pointcut(){

    }
    @Around(value="pointcut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable{

        Object[] args = pjp.getArgs();
        Signature signature = pjp.getSignature();
        long startTime = System.currentTimeMillis();
        Object target = pjp.getTarget();
        log.info("class:{} | method:{} | args:{} | start:{}",
                target.getClass(), signature.getName(), JSONObject.toJSONString(args),startTime);
        try {
            Object o = pjp.proceed(args);
            log.info("class:{} | method:{} | result:{} | cost:{}秒",
                    target.getClass(), signature.getName(),
                    JSONObject.toJSONString(o),
                    (System.currentTimeMillis() -startTime)/1000);
            return o;
        } catch (Exception e) {
            log.error("class:{} | method:{} | cost:{}秒",
                    target.getClass(), signature.getName(),
                    (System.currentTimeMillis() -startTime)/1000,e);
            throw e;
        }

    }

}
